package br.com.aerusc.gui.components;

import com.vaadin.Application;
import com.vaadin.service.ApplicationContext.TransactionListener;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Window;

public class RootComponents implements TransactionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HorizontalSplitPanel horizontalSplitLayout;
	private Window mainWindow;
	private Application app;
	private static ThreadLocal<RootComponents> instance = new ThreadLocal<RootComponents>();

	private RootComponents(Application app) {
		 this.app = app;
		 instance.set(this);
	}

	@Override
    public void transactionStart(Application application, Object transactionData) {
        if(this.app == application) {
            instance.set(this);
        }
    }

	@Override
    public void transactionEnd(Application application, Object transactionData) {
        if(this.app == application) {
            instance.set(null);
        }
    }

	public static void setHorizontalSplitLayout(HorizontalSplitPanel horizontalSplitLayout) {
		instance.get().horizontalSplitLayout = horizontalSplitLayout;
	}

	public static HorizontalSplitPanel getHorizontalSplitLayout() {
		return instance.get().horizontalSplitLayout;
	}
	
	public static void setMainWindow(Window mainWindow) {
		instance.get().mainWindow = mainWindow;
	}

	public static Window getMainWindow() {
		return instance.get().mainWindow;
	}

	public static void initialize(Application application) {
        if(application == null) {
            throw new IllegalArgumentException("Application cannot be null");
        }
        RootComponents root = new RootComponents(application);
        application.getContext().addTransactionListener(root);
    }
}
